package jsyf.oa.service.importExcel;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.importExcel.UserEntityDao;
import jsyf.oa.entity.importExcel.UserEntity;
import jsyf.oa.facade.importExcel.UserTestFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 92852 on 2018/4/17.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class UserTestService implements UserTestFacade {
    @Autowired
    private UserEntityDao userEntityDao;

    @Override
    public String readExcelFile(String jsonData) {
        List<UserEntity> entity = JSONObject.parseArray(jsonData, UserEntity.class);

        for (int i = 0; i < entity.size(); i++) {
            UserEntity model = entity.get(i);
            String name=model.getName();
            String age=model.getAge();
            String sex=model.getSex();

            StringBuffer condition = new StringBuffer();
            condition.append("SELECT COUNT(1) AS count FROM t_sys_user_test \n" +
                    "WHERE 1=1"+"\n");
            condition.append(" AND `NAME`="+" '"+name+"'"+"\n");
            condition.append(" AND `AGE`="+" '"+age+"'"+"\n");
            condition.append(" AND `SEX`="+" '"+sex+"'"+"\n");
            List<JSONObject> records = userEntityDao.queryForJsonList(condition.toString(), new Object[]{});
            JSONObject js= records.get(0);
            int count=js.getInteger("count");
            if (count>0){
                userEntityDao.update(model);
            }else{
                userEntityDao.add(model);
            }

        }
        return "true";
    }



}
